package com.lc.luzhengtang.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.lc.luzhengtang.R;
import com.lc.luzhengtang.conn.GetEvaluate;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by DR on 2016/9/6.
 */
public abstract class EvaluateAdapter extends BaseAdapter implements View.OnClickListener {

    private Context context;

    private ResultCallBack resultCallBack;

    private List<GetEvaluate.EvaluateList> evaluateList = new ArrayList<>();

    public String comment, pic;

    public AddPicAdapter2 addPicAdapter;
    //图片路径集合
    public List<String> mPath;

    public List<String> photo_list;

    public Map<Integer, List<String>> listMap;

    public EvaluateAdapter(Context context, List<GetEvaluate.EvaluateList> evaluateList) {

        this.context = context;

        this.evaluateList = evaluateList;

        listMap = new HashMap<>();


    }

    public void notifyDataSetChanged(List<GetEvaluate.EvaluateList> evaluateList) {

        for (int i = 0; i < evaluateList.size(); i++) {

            photo_list = new ArrayList<>();

            listMap.put(i, photo_list);

        }

        notifyDataSetChanged();

    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();

        for (int i = 0; i < evaluateList.size(); i++) {

            listMap.put(i, listMap.get(i));


        }

    }

    @Override
    public int getCount() {
        return evaluateList.size();
    }

    @Override
    public Object getItem(int position) {
        return evaluateList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;

        if (convertView == null) {

            convertView = LayoutInflater.from(context).inflate(R.layout.item_evaluate2, null);

            //  BaseApplication.ScaleScreenHelper.loadView((ViewGroup) convertView);
            ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) convertView);
            holder = new ViewHolder();
            //商品名称
            holder.tv_evaluate_name = (TextView) convertView.findViewById(R.id.tv_evaluate_name);
            //商品单价
            holder.tv_evaluate_price = (TextView) convertView.findViewById(R.id.tv_evaluate_price);
            //商品图片
            holder.img_evaluate = (ImageView) convertView.findViewById(R.id.img_evaluate);
            //相机按钮
            holder.img_camera = (ImageView) convertView.findViewById(R.id.img_camera);

            holder.et_evaluate = (EditText) convertView.findViewById(R.id.et_evaluate);


            holder.gv_pic = (GridView) convertView.findViewById(R.id.gv_pic);

            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();

        }

        final GetEvaluate.EvaluateList evaluateEntity = evaluateList.get(position);

        holder.et_evaluate.setTag(evaluateEntity);

        holder.et_evaluate.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                try {
                    GetEvaluate.EvaluateList evaluateEntity = (GetEvaluate.EvaluateList) holder.et_evaluate.getTag();
                    evaluateEntity.etStr = s + "";
                } catch (Exception e) {
                }

            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        //大部分情况下，getview中有if必须有else
        if (!TextUtils.isEmpty(evaluateEntity.etStr)) {
            holder.et_evaluate.setText(evaluateEntity.etStr);
        } else {
            holder.et_evaluate.setText("");
        }


//

        addPicAdapter = new AddPicAdapter2(context, listMap.get(position)) {
            @Override
            public void onPhoto() {

            }

            @Override
            public void deletePhoto() {

            }
        };
/*
        holder.gv_pic.setAdapter(addPicAdapter);

        holder.gv_pic.setFocusable(false);

        if (listMap.get(position).size() > 0) {

            holder.gv_pic.setVisibility(View.VISIBLE);

        } else {

            holder.gv_pic.setVisibility(View.GONE);

        }*/

        holder.img_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                onClickItem(position);

            }
        });

        holder.tv_evaluate_name.setText(evaluateList.get(position).goodsname);

        holder.tv_evaluate_price.setText("¥" + evaluateList.get(position).price);

        //  Picasso.with(context).load(Conn.IMG + evaluateList.get(position).picurl).placeholder(R.mipmap.tp).into(holder.img_evaluate);

        pic = photo_list.toString();

        return convertView;

    }

    @Override
    public void onClick(View v) {

    }

    private class ViewHolder {
        //商品名称，商品单价
        public TextView tv_evaluate_name, tv_evaluate_price;
        //商品图片，相机按钮
        public ImageView img_evaluate, img_camera;

        public EditText et_evaluate;

        public GridView gv_pic;

    }

    public void RefreshAdapter() {

    }

    public abstract void onClickItem(int position);

    public interface ResultCallBack {

        void onResultCallBack(JSONArray array);

    }

}
